package com.zadtek.shopmall.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.activities.ApplyServiceActivity;
import com.zadtek.shopmall.base.AbsListFragment;
import com.zadtek.shopmall.bean.ProcessItem;
import com.zadtek.shopmall.bean.ProductItem;
import com.zadtek.shopmall.bean.RCItem;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.zadtek.shopmall.activities.RedEnvelopActivity.TAB_1;
import static com.zadtek.shopmall.activities.RedEnvelopActivity.TAB_2;

/**
 * Created by Zackv on 2018/4/11.
 */

public class AfterMarketFragment extends AbsListFragment {
    private int currentTabIndex = TAB_1;

    private BaseQuickAdapter rcAdapter;
    private List<RCItem> data = new LinkedList<>();
    private List<ProcessItem> data2 = new LinkedList<>();
    private int page = 1;
    private int pagesize =10;

    public AfterMarketFragment(){

    }

    public static AfterMarketFragment Instance(int tabIndex){
        AfterMarketFragment instance = new AfterMarketFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("tabIndex",tabIndex);
        instance.setArguments(bundle);
        return instance;
    }
    @Override
    protected void firstRequestData() {
        switch (currentTabIndex){
            case TAB_1:
                loadDataTab1();
                break;
            case TAB_2:
                loadDataTab2();
                break;
        }

    }

    private void loadDataTab2() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("pagesize",pagesize);
        NetTool.getApi().getProcess(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<ProcessItem>>>() {
                    @Override
                    public void onData(BaseResponse<List<ProcessItem>> value) {
                        if(value.getCode() == 0 && value.getData()!= null){
                            if(value.getData().size()!=0){
                                data2.addAll(value.getData());
                                rcAdapter.loadMoreComplete();
                                rcAdapter.notifyDataSetChanged();
                            }else{
                                rcAdapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    private void loadDataTab1() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("pagesize",pagesize);
        NetTool.getApi().getAfterMarket(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<RCItem>>>() {
                    @Override
                    public void onData(BaseResponse<List<RCItem>> value) {
                        if(value.getCode() == 0 && value.getData()!= null){
                            if(value.getData().size()!=0){
                                data.addAll(value.getData());
                                rcAdapter.loadMoreComplete();
                                rcAdapter.notifyDataSetChanged();
                            }else{
                                rcAdapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    @Override
    protected void loadMore() {
        switch (currentTabIndex){
            case TAB_1:
                loadDataTab1();
                break;
            case TAB_2:
                loadDataTab2();
                break;
        }
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        Bundle bundle = getArguments();
        if(bundle!=null){
            currentTabIndex = bundle.getInt("tabIndex");
        }
        switch (currentTabIndex){
            case TAB_1:
                rcAdapter = new RcAdapter(R.layout.after_market_item,data);
                break;
            case TAB_2:
                rcAdapter = new ProcessAdapter(R.layout.aftermarket_item3,data2);
                break;
        }
        rcAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_btn_apply:
                        //查看详情按钮
                        ToastUtil.showToast("查看详情！");
                        break;
                    case R.id.tv_btn_cancel:
                        //取消按钮
                        ToastUtil.showToast("取消按钮！");
                        break;
                }
            }
        });
        return rcAdapter;
    }

    @Override
    protected void OnItemClick(int position) {
        ToastUtil.showToast("click"+position);
    }

    @Override
    protected void OnRefresh() {
        setRefreshing(false);
    }
    private class RcAdapter extends BaseQuickAdapter<RCItem,BaseViewHolder>{

        public RcAdapter(int layoutResId, @Nullable List<RCItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final RCItem item) {
            RecyclerView recyclerView = helper.getView(R.id.recycleview);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            RcInnerAdapter innerAdapter = new RcInnerAdapter(R.layout.aftermarket_item2,item.getGoods_list());
            innerAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    switch (view.getId()){
                        case R.id.tv_btn_apply:
                            ToastUtil.showToast("你点击的Id为："+item.getGoods_list().get(position).getProductId());
                            startActivity(new Intent(mActivity,ApplyServiceActivity.class).putExtra("data",item.getGoods_list().get(position)));
                            break;
                    }
                }
            });
            recyclerView.setAdapter(innerAdapter);
            helper.setText(R.id.tv_order_id,item.getOrder_sn());
            helper.setText(R.id.tv_order_time,item.getAdd_time());
        }
    }

    private class RcInnerAdapter extends BaseQuickAdapter<ProductItem,BaseViewHolder>{

        public RcInnerAdapter(int layoutResId, @Nullable List<ProductItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ProductItem item) {
            helper.addOnClickListener(R.id.tv_btn_apply);
            helper.setText(R.id.tv_product_desc,item.getGoods_name());
            helper.setText(R.id.tv_product_num,"数量 X"+item.getGoods_number());
            Glide.with(mActivity).load(item.getGoods_thumb()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_product_img));
        }
    }

    private class ProcessAdapter extends BaseQuickAdapter<ProcessItem,BaseViewHolder>{

        public ProcessAdapter(int layoutResId, @Nullable List<ProcessItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ProcessItem item) {
            helper.addOnClickListener(R.id.tv_btn_cancel);
            helper.addOnClickListener(R.id.tv_btn_apply);
            helper.setText(R.id.tv_order_id,item.getOrderid());
            helper.setText(R.id.tv_order_time,item.getOrdertime());
            helper.setText(R.id.tv_product_desc,item.getProductTitle());
            helper.setText(R.id.tv_product_num,"数量 X"+item.getProductNum());
            Glide.with(mActivity).load(item.getProductPic()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_product_img));
        }
    }
}
